#include "c2queue.h"
 

struct Customer* head = NULL;
struct Customer* curr = NULL;
int listnum = 0;
struct Customer* temp;
struct Customer* blankCust;
 
 
 int main(){
    char line[200];
    char *opt;
    char *idnum;
    char *firstname;
    char *lastname;
    int id;
    int i = 0;
    FILE *datfile;
    int inbytes;
        
    
    if(loadData() == 0)
        printf("queue.dat was not loaded\n");
    

      while(i == 0){
        fgets(line,sizeof(line), stdin);

        opt = strtok(line," \t\n");
       
        if(strcmp(opt,"a")==0 | strcmp(opt, "A")==0){
           
            idnum = strtok(NULL," \t\n");
            firstname = strtok(NULL," \t\n");
            lastname = strtok(NULL," \t\n");

            if(!idnum | !firstname | !lastname)
                printf("ERROR\n");
            else{
                       
                id = atoi(idnum);
                

            if(id < 0 | id > INT_MAX | (strlen(firstname) > 50) | (strlen(lastname) > 50))
                printf("ERROR\n");
            else{
                temp = malloc(sizeof(struct Customer));
                (*temp).id = id;
                strcpy((*temp).first,firstname);
                strcpy((*temp).last,lastname);
                (*temp).next_ptr = NULL;

                if(idCheck(id) == -1){
                    free(temp);
                    printf("ERROR\n");
                }
                else if(addCust(temp) == 0){
                    free(temp);
                    printf("ERROR\n");
                }else
                    printf("OK\n");
                } 
            }
        }

        else if(strcmp(opt, "r")==0 | strcmp(opt, "R")==0){
            if(custCount() == 0)
                printf("ERROR\n");
            else{
                head = remCust();
                

                
            }

        }

        else if(strcmp(opt, "c")==0 | strcmp(opt, "C")==0){
            printf("%d\n",custCount());
        }

        else if(strcmp(opt, "e")==0 | strcmp(opt, "E")==0){
            if(saveData() == 0){
                printf("Error: unable to save queue.dat\n");
                return EXIT_FAILURE;
            }
            return EXIT_SUCCESS;
        }

        else{
            printf("ERROR\n");
        }
    }
}




int addCust(struct Customer* cust){
    struct Customer* temp;
    if(head == NULL){
        (*cust).next_ptr = head;
        head = cust;
        listnum++;
    }
    else{
        temp = head;
        while (temp != NULL) {
            if((*temp).next_ptr == NULL){
                (*temp).next_ptr = cust;
                break;
            }
            else {
                temp = (*temp).next_ptr;
                continue;
            }
        }
        listnum++;
        
    }
    return 1;
}

struct Customer* remCust(){
    struct Customer* temp_head;
    temp_head = (*head).next_ptr;
    printf("ID: %d\nFirst: %s\nLast: %s\n",(*head).id,(*head).first,(*head).last);
    if(temp_head == NULL)
        head = NULL;    
    free(head);
    listnum--;
    
    return temp_head;
    
}

 int custCount(){
     return listnum;
 }


 int saveData(){
     int fd,i,n;
     struct Customer* temp;
     

     temp = head;

     if((fd = open("queue.dat", O_TRUNC | O_WRONLY | O_CREAT, 0777)) < 0)
         return 0;
    
     while(temp != NULL){
        n = write(fd,temp,sizeof(struct Customer));
        if(n == 0){
            printf("an error occured while writing to queue.dat\n");
            return 0;
        }
        temp = (*temp).next_ptr; 
    }
    temp = malloc(sizeof(struct Customer));
    (*temp).id = -1;
    write(fd,temp,sizeof(temp));
    free(temp);
    return 1;
 }

 int loadData(){
     int fd,i,custNum,n;
     struct Customer* temp;
     
     if((fd = open("queue.dat", O_RDONLY)) < 0)
         return 0;
           
     
     while(1){

         temp = malloc(sizeof(struct Customer));
         n = read(fd,temp,sizeof(struct Customer));
         if ((*temp).id == -1) {
             break;
         }
         if(n < 0){
             printf("file is the wrong format");
             return 0;
         }
         addCust(temp);
         

     }
        
     

     return 1;
 }

int idCheck(int id){
    struct Customer *temp_ptr;
    temp_ptr = head;
    while(temp_ptr != NULL){
        if((*temp_ptr).id == id){
            return -1;
        }
        temp_ptr = (*temp_ptr).next_ptr; 
    }
    return 0;

}
